import "fmt"

/*
 * @lc app=leetcode.cn id=54 lang=golang
 *
 * [54] 螺旋矩阵
 */

// @lc code=start
func spiralOrder(matrix [][]int) []int {
	top := 0
	bottom := len(matrix) - 1
	left := 0
	right := len(matrix[0]) - 1
	tmp := []int{}
	for top < bottom && left < right {
		for i := left; i < right; i++ {
			fmt.Println(matrix[top][i])
			tmp = append(tmp, matrix[top][i])
		}

		for i := top; i < bottom; i++ {
			fmt.Println(matrix[i][right])
			tmp = append(tmp, matrix[i][right])
		}

		for i := right - 1; i >= left; i-- {
			fmt.Println(matrix[bottom][i])
			tmp = append(tmp, matrix[bottom][i])
		}

		for i := bottom - 1; i > top; i-- {
			fmt.Println(matrix[i][left])
			tmp = append(tmp, matrix[i][left])
		}
		top++
		bottom--
		left++
		right--
	}

	if left == right {
		for i := top; i <= bottom; i++ {
			fmt.Println(matrix[i][left])
			tmp = append(tmp, matrix[i][left])
		}
	} else if bottom == top {
		for i := left; i <= right; i++ {
			fmt.Println(matrix[top][i])
			tmp = append(tmp, matrix[top][i])
		}
	}
	//for top < bottom && left < right {
	//	for i := 0; i < right; i++ {
	//		fmt.Println(matrix[top][i])
	//	}
	//}
	return tmp
}

// @lc code=end

